package controller;

import pojo.User;
import service.UserService;
import view.SelfUserDetails;
import view.SelfUserView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SelfUserDetailsListener implements ActionListener {
    private SelfUserDetails selfUserDetails;
    private SelfUserView selfUserView;
    public SelfUserDetailsListener(SelfUserDetails selfUserDetails,SelfUserView selfUserView){
        this.selfUserDetails=selfUserDetails;
        this.selfUserView=selfUserView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("返回")) {
            selfUserDetails.dispose();
        }
        if (e.getActionCommand().equals("修改")) {
            UserService userService = new UserService();
            String userId= selfUserDetails.getTfNum().getText();
            String userName= selfUserDetails.getTfName().getText();
            String userGender= selfUserDetails.getTfGender().getText();
            String userPN= selfUserDetails.getTfPN().getText();
            String userType= selfUserDetails.getTfType().getText();
            String passWord= selfUserDetails.getUser().getPassWord();
            User user = new User(userId,userName,passWord,userGender,userPN,userType);
            int a = JOptionPane.showConfirmDialog(selfUserDetails, "确认修改?", "确认信息", JOptionPane.YES_NO_OPTION);
            if (a == JOptionPane.YES_OPTION) {
                userService.update(user);
                selfUserDetails.dispose();
                selfUserView.refresh(selfUserDetails.getTfNum().getText());
        }
    }
}}
